/*
 * Copyright (c) 2020-2022 Jinsp-tech. All rights reserved.
 */

package com.jinsp.networklibrary.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

/**
 * Created by luoshuai on 1/13/22
 * <p>
 * 网络工具类
 */
public class NetworkUtils {

    /**
     * 判断IP地址是否合法
     *
     * @param ip 服务端IP地址
     * @return true -- 合法IP
     */
    public static boolean isIPLegal(String ip) {
        String[] strArr = ip.split("\\.");
        if (strArr.length != 4) {
            return false;
        }
        for (String everyNumber : strArr) {
            int number = Integer.parseInt(everyNumber);
            if (number < 0 || number > 255) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断WiFi是否开启
     *
     * @param context 上下文
     * @return true -- 已开启WiFi
     */
    public boolean isWIFIOpened(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        return wifiManager != null && wifiManager.isWifiEnabled();
    }

    /**
     * 开启WiFi
     *
     * @param context 上下文
     */
    public void openWIFI(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
    }

    /**
     * 判断网络是否可用
     *
     * @param context 上下文
     * @return true -- 网络可用
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
        }
        return false;
    }

    /**
     * 判断是否为WiFi
     *
     * @param context 上下文
     * @return true -- 是WiFi状态
     */
    public static boolean isWIFIState(Context context) {
        return getNetworkType(context) == ConnectivityManager.TYPE_WIFI;
    }

    /**
     * 是否为移动网络
     *
     * @param context 上下文
     * @return true -- 是移动网络
     */
    public static boolean isMobileState(Context context) {
        return getNetworkType(context) == ConnectivityManager.TYPE_MOBILE;
    }

    /**
     * 获取网络类型
     *
     * @param context 上下文
     * @return WiFi or GPRS
     */
    public static int getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo == null ? -1 : activeNetworkInfo.getType();
        }
        return -1;
    }
}
